package control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Quart;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	import utils.MyTimer;
	
	public class SlideShow extends EventDispatcher
	{
		protected var con:DisplayObjectContainer;
		protected var bitmaps:Array;
		protected var slideTime:Number=3;
		protected var max_width:Number;
		protected var max_height:Number;
		protected var slideTimer:MyTimer;
		protected var prev_bt:Sprite;
		protected var next_bt:Sprite;
		protected var lastBitmap:DisplayObject;
		protected var curNum:uint=0;
		public function SlideShow(_con:DisplayObjectContainer,_bitmaps:Array,_max_width:Number,_max_height:Number,_slideTime:Number=3,_prev_bt:Sprite=null,_next_bt:Sprite=null,target:IEventDispatcher=null)
		{
			super(target);
			con=_con;
			bitmaps=_bitmaps;
			max_width=_max_width;
			max_height=_max_height;
			slideTime=_slideTime;
			prev_bt=_prev_bt;
			next_bt=_next_bt;
			init();
		}
		
		protected function init():void
		{
			if(prev_bt)
			{
				addBtEvent();
			}
			showNext();
			addTimer();
		}
		
		protected function addTimer():void
		{
			if(!slideTimer)
			{
				slideTimer=new MyTimer(showNextByTimer,slideTime);
				slideTimer.start();
			}
		}
		
		protected function removeTimer():void
		{
			if(slideTimer)
			{
				slideTimer.GC();
				slideTimer=null;
			}
		}
		
		protected function positionBts():void
		{
			if(prev_bt)
			{
				//如果设置了按钮
				prev_bt.x=50;
				next_bt.x=max_width-next_bt.width-50;
				prev_bt.y=max_height/2-prev_bt.height/2;
				next_bt.y=prev_bt.y;
				if(con.contains(prev_bt))
				{
					//如果包含按钮
					
				}
				else
				{
					//如果不包含按钮
					if(!prev_bt.parent)
					{
						con.addChild(prev_bt);
						con.addChild(next_bt);
					}
				}
				con.addChildAt(lastBitmap,0);//放在最下面
			}
			else
			{
				con.addChild(lastBitmap);
			}
		}
		
		protected function showNextByTimer():void
		{
			curNum++;
			if(curNum==bitmaps.length)
			{
				curNum=0;
			}
			
			showNext();
		}
		
		protected function showNext():void
		{
			addTimer();
			hideLast(lastBitmap);
			var mc:DisplayObject=bitmaps[curNum];
			mc.alpha=0;
			mc.x=max_width/2-mc.width/2;
			mc.y=max_height/2-mc.height/2;
			TweenLite.to(mc,1,{alpha:1,delay:0.5,ease:Quart.easeOut});
			lastBitmap=mc;
			positionBts();
		}
		
		protected function hideLast(mc:DisplayObject):void
		{
			if(mc)
			{
				TweenLite.to(mc,1,{alpha:0,ease:Quart.easeOut,onComplete:removeLast,onCompleteParams:[mc]});
			}
		}
		
		protected function removeLast(mc:DisplayObject):void
		{
			if(mc.parent)
			{
				mc.parent.removeChild(mc);
			}
		}
		
		
		protected function removeBitmaps():void
		{
			if(bitmaps)
			{
				var length:uint=bitmaps.length;
				for(var i:uint=0;i<length;i++)
				{
					var bm:Bitmap;
					var mc:DisplayObject=bitmaps[i];
					if(bitmaps[i] is Bitmap)
					{
						bm=mc as Bitmap;
						bm.bitmapData.dispose();
					}
					if(mc is DisplayObjectContainer)
					{
						var _con:DisplayObjectContainer=mc as DisplayObjectContainer;
						
						for(var o:uint=0;o<_con.numChildren;o++)
						{
							bm=_con.getChildAt(o) as Bitmap;
							if(bm)
							{
								bm.bitmapData.dispose();
							}
						}
					}
				}
				bitmaps=null;
			}
		}
		
		protected function addBtEvent():void
		{
			
			prev_bt.addEventListener(MouseEvent.CLICK,handleBtClick);
			next_bt.addEventListener(MouseEvent.CLICK,handleBtClick);
		}
		
		protected function handleBtClick(e:MouseEvent):void
		{
			removeTimer();
			var mc:Sprite=e.currentTarget as Sprite;
			if(mc==prev_bt)
			{
				if(curNum==0)
				{
					curNum=bitmaps.length-1;
				}
				else
				{
					curNum--;
				}
				
			}
			else
			{
				if(curNum==bitmaps.length-1)
				{
					curNum=0;
				}
				else
				{
					curNum++;
				}
			}
			showNext();
		}
		
		protected function removeBtEvent():void
		{
			prev_bt.removeEventListener(MouseEvent.CLICK,handleBtClick);
			next_bt.removeEventListener(MouseEvent.CLICK,handleBtClick);
			if(prev_bt.parent)
			{
				prev_bt.parent.removeChild(prev_bt);
			}
			if(next_bt.parent)
			{
				next_bt.parent.removeChild(next_bt);
			}
			prev_bt=null;
			next_bt=null;
		}
		
		
		public function gc():void
		{
			if(prev_bt)
			{
				removeBtEvent();
			}
			removeTimer();
			con.removeChildren();
			removeBitmaps();
		}
		
		
		
	}
}